function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds();


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function login(obj){
   
    var token = wx.getStorageSync("login");
    console.log(token);
      //检查token是否过期
      wx.request({
        url:getApp().rootUrl+"api/islogin",
        data:{token:token},
        method:"GET",
        success:function(responseData){
           if(responseData.data.state == "ok"){
             obj.success(token);
           }else{
             wx.login({
               success: function (res) {
                 if (res.code) {
                   wx.request({
                     url: 'https://www.myfoxcloud.com/api/login',
                     data: { code: res.code },
                     success: function (responseData) {
                       console.log(responseData);
                       if (responseData.data.state == 'ok') {
                         wx.setStorage({
                           key: 'login',
                           data: responseData.data.token
                         });
                         obj.success(responseData.data.token);
                       }
                     }
                   })
                 }
               }
             });
           }
        }
      });
  
}

module.exports = {
  formatTime: formatTime,
  foxlogin:login,

}
